<template>
    <div v-cloak>
        <template>
            <div class="align-right">
              <el-form :inline="true" :model="listQuery">
                  <div class="pull-right">
                    <el-form-item>
                        <el-input
                            placeholder="请输入分类名称"
                            clearable
                            v-model="listQuery.name">
                        </el-input>
                    </el-form-item>
                    <el-form-item>
                      <el-button type="primary" @click="handleSearch" icon="el-icon-search" >搜索</el-button>
                    </el-form-item>
                  </div>
                <div>
                    <el-button  @click="add">新增分类</el-button>
                </div>
              </el-form>
            </div> 
            <div class="table-container">
            <el-table
                ref="classifyTable"
                :data="dataClassify"
                row-key="id"
                border
                highlight-current-row
                @row-click="RowClick"
                v-bind:class="{'is-edit': isEdit}"
                style="width: 100%">

                <el-table-column align="center" label="序号" width ="100" >
                    <template slot-scope="scope">
                        <span >{{ scope.$index+1 }}</span>
                    </template>
                    </el-table-column>
                <el-table-column
                align="center"
                label="分类名称">
                <template slot-scope="scope">
                    <el-input v-model.trim="scope.row.name" placeholder="分类名称不能超过四个字" maxlength="4"></el-input>
                    <span>{{ scope.row.name }}</span>
                </template>
                </el-table-column>
                <el-table-column
                    align="center"
                    label="是否可用"
                    width="">
                    <template slot-scope="scope">
                    <span >{{scope.row.enable?'是':'否'}}</span>
                    <el-select  v-model="scope.row.enable">
                    <el-option
                        v-for="item in options"
                        :key="item.id"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                    </el-select>
                    </template>
                </el-table-column>
                <el-table-column
                    align="center"
                    width="200px"
                    label="操作"
                    >
                <template slot-scope="scope">
                <div class="default">
                <el-button type="text"  @click="edit(scope)">编辑</el-button>
                <el-button type="text" @click="remove(scope)">删除</el-button>
            </div>
            <div class="confirm">
                <el-button type="text" @click="submit(scope)">确认</el-button>
                <el-button type="text" @click="cancle(scope)">取消</el-button>
            </div>
                </template>
                </el-table-column>
            </el-table>
            </div>
            <el-col :span="24" class="pagination-warp" v-if="dataClassify.length>0">
            <div class="text-center">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.currentPage"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="listQuery.size"
                    layout="total, sizes, prev, pager, next ,jumper"
                    :total="total">
                </el-pagination>
                </div>
            </el-col>
        </template>
    </div>
</template>
<script>
import { getClassifies, insertClassify, updateClassify,　delClassify } from '@/api/sys/classify'
export default {
    data() {
        return {
            dataClassify: [],
            options: [
                {id:1,label:"是",value:1},
                {id:2,label:"否",value:0}
            ],
            tempRow: {},
            total: null,
            adding: false,
            isEdit: false,
            listQuery: {
                currentPage: 1,
                size: 10
            }
        }
    },
    created(){
        this.getList()
    },
    methods: {
        getList() {
            const params = this.listQuery
            getClassifies(params).then((response) => {
                if (response.code === 0) {
                    this.dataClassify = response.data.records
                    this.total = response.data.total
                    this.isEdit = false
                    this.adding = false
                }
            })
        },
        handleSearch(){

        },
        handleSizeChange(val) {
            this.listQuery.currentPage = 1
            this.listQuery.size = val
            this.getList()
        },
        handleCurrentChange(val) {
            this.listQuery.currentPage = val
            this.getList()
        },
        RowClick(row, eve ,column) {
            if(row.id === "") return
            if(row.id !== this.tempRow.id) {
                this.isEdit = false
            }
            if (this.adding) {
                this.$nextTick(function () {
                    this.classifyDataLabel.splice(0,1)
                })
                this.adding = false
            }
        },
        add() {
            if(this.adding) return
            const newRow = {id:"", name:"",enable:1}
            this.dataClassify.unshift(newRow)
            this.recoveryHighLight(newRow)
            this.isEdit = true
            this.adding = true
        },
        edit(scope) {
            this.isEdit = true
            this.tempRow = Object.assign({}, this.tempRow, scope.row)
        },
        submit(scope) {
            const row = scope.row
            if (row.id === "") {
                insertClassify(row).then((response) => {
                    if (response.code === 0) {
                        this.isEdit = false
                        this.adding = false
                        this.getList()
                        this.$message({
                            type: 'success',
                            message: response.msg
                        })
                    }else{
                         this.$message({
                            type: 'error',
                            message: response.msg
                        })
                    }
                })
            } else {
                updateClassify(row).then((response) => {
                    if (response.code === 0) {
                        this.isEdit = false
                        this.getList()
                        this.$message({
                            type: 'success',
                            message: response.msg
                        })
                    }else{
                         this.$message({
                            type: 'error',
                            message: response.msg
                        })
                    }
                })
            }

        },
        cancle(scope) {
            const row = scope.row
            if (row.id === "") {
                this.getList()
                this.adding = false
            }
            this.isEdit = false
        },
        remove(scope) {
            const row = scope.row
            row.delFlag = 1
            this.$confirm('此操作将删除该行数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    delClassify(row).then((response) => {
                        if(response.code === 0) {
                            this.getList()
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            })
                        }
                    })
                }).catch(() => {
                  
                })
        },
        recoveryHighLight(row){
            this.$nextTick(function () {
            this.$refs.classifyTable.setCurrentRow(row)
            })
        }
    }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.table-container .el-input,.el-select,.confirm{
    display: none;
}
.is-edit .current-row .default {
    display: none
}
.is-edit .current-row span {
    display: none
}
.is-edit .current-row .confirm{
    display: block;
}
.is-edit .current-row .el-select {
    display: block
}
.is-edit .current-row .el-input {
    display: block
}
.table-search{
  margin: 10px;
  .el-input{
    width: 200px;
    margin-right: 6px;
  }
}
.table-page{
    width: 700px;
    margin: 5px 32%;
}
</style>

